//
//  PageFlowMenuTableViewCell_SizeFitsPage.m
//  MKUI
//
//  Created by 苏尚进 on 2020/10/10.
//Copyright © 2020 moon. All rights reserved.
//

#import "PageFlowMenuTableViewCell_SizeFitsPage.h"
#import "MenuCollectionViewCell_SizeFits.h"

@interface PageFlowMenuTableViewCell_SizeFitsPage ()

@end

@implementation PageFlowMenuTableViewCell_SizeFitsPage
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
	if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.pageFlowLayout.pagingEnabled = YES;
        self.pageFlowLayout.pagingCellPosition = 0;
        self.pageFlowLayout.pagingBoundsPosition = 0;
        self.pageFlowLayout.interitemSpacing = 30;
        self.pageFlowLayout.enableCycleScroll = YES;
        self.collectionView.model.forwardDelegate = self;
        @MK_WEAKIFY(self);
        MKUILayoutConstraintItemWrapper *w1 = [MKUILayoutConstraintItemWrapper wrapItem:self.collectionView sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            @MK_NORMALIZE(self);
            CGSize s = [self.pageFlowLayout mk_sizeThatFits:size];
            s.width = size.width;
            return s;
        }];
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[w1,self.pageControl] constraintParam:(MKUIFlowLayoutConstraintParam_V_C_C) contentInsets:UIEdgeInsetsZero interitemSpacing:5];
    }
	return self;
}
- (void)layoutMyContentView{
    [super layoutMyContentView];
    CGRect bounds = self.myContentView.bounds;
    self.flowlayout.bounds = bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeMyContentViewThatFits:(CGSize)size{
    return [self.flowlayout sizeThatFits:size resizeItems:YES];
}
+ (Class)menuCellClass{
    return [MenuCollectionViewCell_SizeFits class];
}
- (void)setCellModel:(__kindof MKUITableViewCellModel *)cellModel{
    [super setCellModel:cellModel];
}
- (CGSize)collectionView:(UICollectionView *)collectionView pageFlowLayout:(MKUICollectionViewPageFlowLayout *)collectionViewLayout itemSizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGSize size;
    MKUICollectionViewCellModel *cm = [self.collectionView.model cellModelAtIndexPath:indexPath];
    size = [MenuCollectionViewCell_SizeFits dynamicSizeWithCollectionView:collectionView collectionCellModel:cm cellShareInstance:[MenuCollectionViewCell_SizeFits sharedInstance] calBlock:^CGSize(UICollectionView * _Nonnull collectionView, MKUICollectionViewCellModel * _Nonnull cellModel, MenuCollectionViewCell_SizeFits *cell) {
        CGRect bounds = collectionView.bounds;
        return [cell sizeThatFits:CGSizeMake(bounds.size.width/2, bounds.size.height)];
    }];
//    NSLog(@"collection size:%@,%@",NSStringFromCGSize(size),indexPath);
    return size;
}
- (UIEdgeInsets)sectionInsets{
    UIEdgeInsets insets = [super sectionInsets];
    insets.bottom = 0;
    return insets;
}
@end
